import axios from 'axios'
import { API_BASE_URL } from '../config/api'

// 获取资源（权限）列表（POST，x-www-form-urlencoded，支持分页和条件参数）
export function getResourceList(params = {}) {
  const formData = new URLSearchParams()
  Object.entries(params).forEach(([key, value]) => {
    if (value !== undefined && value !== null && value !== '') {
      formData.append(key, value)
    }
  })
  return axios.post(`${API_BASE_URL}/actions/resourceManage/getResourceList`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 新增/更新资源（权限）
export function saveOrUpdateResource(data) {
  return axios.post(`${API_BASE_URL}/actions/resourceManage/saveOrUpdateResource`, data, {
    headers: { 'Content-Type': 'application/json' }
  })
}

// 根据角色ID获取资源（权限）列表
export function getResourceListByRole(roleId) {
  const formData = new URLSearchParams()
  formData.append('roleId', roleId)
  return axios.post(`${API_BASE_URL}/actions/resourceManage/getResourceListByRole`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 分配权限给角色（支持批量），接口为 assignResourceToRole，参数 roleId, resourceId
export function assignResourceToRole({ roleId, resourceId }) {
  const formData = new URLSearchParams()
  formData.append('roleId', roleId)
  formData.append('resourceId', resourceId)
  return axios.post(`${API_BASE_URL}/actions/resourceManage/assignResourceToRole`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 删除权限
export function deleteResource(resourceId) {
  const formData = new URLSearchParams()
  formData.append('resourceId', resourceId)
  return axios.post(`${API_BASE_URL}/actions/resourceManage/deleteResource`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 获取字典表列表
export function getDictionaryTypeList(params = {}) {
  const formData = new URLSearchParams()
  Object.entries(params).forEach(([key, value]) => {
    if (value !== undefined && value !== null && value !== '') {
      formData.append(key, value)
    }
  })
  return axios.get(`${API_BASE_URL}/actions/dictionaryManage/type/list`, {
    params: params,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 新增字典表
export function addDictionaryType(data) {
  return axios.post(`${API_BASE_URL}/actions/dictionaryManage/type/add`, data, {
    headers: { 'Content-Type': 'application/json' }
  })
}

// 获取字典项列表
export function getDictionaryDetailList(dictCode) {
  return axios.get(`${API_BASE_URL}/actions/dictionaryManage/detail/list`, {
    params: { dictCode },
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 新增字典项
export function addDictionaryDetail(data) {
  return axios.post(`${API_BASE_URL}/actions/dictionaryManage/detail/add`, data, {
    headers: { 'Content-Type': 'application/json' }
  })
}

// 删除字典项
export function deleteDictionaryDetail(id) {
  const formData = new URLSearchParams()
  formData.append('id', id)
  return axios.post(`${API_BASE_URL}/actions/dictionaryManage/detail/delete`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
}

// 删除字典表
export function deleteDictionaryType(id) {
  const formData = new URLSearchParams()
  formData.append('id', id)
  return axios.post(`${API_BASE_URL}/actions/dictionaryManage/type/delete`, formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
} 